package org.mrjbee.share.it.easy.client.daemon.mbeans;

import org.mrjbee.share.it.easy.client.daemon.command.Command;
import org.mrjbee.share.it.easy.client.daemon.command.CommandType;

public class RemoteControlImpl implements RemoteControl {

	private Command sharedCommand;
	
	public RemoteControlImpl(Command sharedCommand) {
		this.sharedCommand = sharedCommand;
	}

	public int exec(String command) {
		CommandType type;
		try {
			type = CommandType.valueOf(command);
		} catch (IllegalArgumentException e) {
			return 2;
		}
		
		if (sharedCommand.isInProgress()){
			return 1;
		}
		
		
		synchronized (sharedCommand) {
			sharedCommand.setType(type);
			sharedCommand.process();
		}
		
		return 0;
	}

}
